<template>
	<view>
		<view class="cover">
			<image :src="bannerPicture" mode=""></image>
		</view>
		<view class="topBar">
			<u-tabs-swiper ref="uTabs" :list="list" :current="tabCurrent" @change="tabsChange" :is-scroll="false" swiperWidth="750"></u-tabs-swiper>
		</view>
		<swiper :current="swiperCurrent" @transition="transition" @animationfinish="animationfinish">
			<swiper-item v-for="item in subTypeList">
				<scroll-view scroll-y="true" @scrolltolower="toButtom">
					<view class="content">
						<view class="goodsList">
								<navigator url="" v-for="item in item.goodsItems.items" :key="item.id">
									<image :src="item.picture" mode=""></image>
									<text class="title">{{item.name}}</text>
									<text class="price">￥{{item.price}}</text>
								</navigator>
						</view>
						<view class="load">
							<u-loadmore :status="status" margin-buttom="0rpx"/>
						</view>
					</view>
				</scroll-view>
			</swiper-item>
		</swiper>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list: [],
				typeList: [
					{type: '1', title: '特惠推荐', src: '/hot/preference'},
					{type: '2', title: '爆款推荐', src: '/hot/inVogue'},
					{type: '3', title: '一站买全', src: '/hot/oneStop'},
					{type: '4', title: '新鲜好物', src: '/hot/new'}
				],
				selectList: {},
				tabCurrent: 0,
				swiperCurrent: 0,
				status: 'loadmore',
				bannerPicture: '',
				subTap: '',
				subTypeList: [],
				info: {},
				result: {}
			}
		},
	     onLoad(options) {
			this.selectList = this.typeList.find(item => item.type === options.type)
			console.log(this.selectList)
			this.info = {}
			this.getDataStart()
			uni.setNavigationBarTitle({
				title:this.selectList.title
			})
		},
		methods: {
			tabsChange(index){
				// this.tabCurrent = index
				this.swiperCurrent = index
			},
			transition(e){
				let dx = e.detail.dx
				this.$refs.uTabs.setDx(dx);
			},
			animationfinish(e){
				this.swiperCurrent = e.detail.current
				this.tabCurrent = e.detail.current
			},
			async toButtom(){
				this.status = 'loading'
				const nowSubtapes = this.subTypeList[this.tabCurrent]
				nowSubtapes.goodsItems.page++
				const res = await this.$http({
					url: this.selectList.src,
					data: {
						subType: nowSubtapes.id,
						page: nowSubtapes.goodsItems.page
					}
				})
				console.log(res.data)
				nowSubtapes.goodsItems.items.push(...res.data.result.subTypes[this.tabCurrent].goodsItems.items)
				if(nowSubtapes.goodsItems.page < nowSubtapes.goodsItems.pages){
					this.status = 'loadmore'
				}else{
					this.status = 'nomore'
				}
			},
			async getDataStart(){
				const res = await this.$http({
					url: this.selectList.src
				})
				console.log(res)
				this.bannerPicture = res.data.result.bannerPicture
				this.subTypeList = res.data.result.subTypes
				this.list = this.subTypeList.map(item => ({name:item.title}))
				console.log(this.list)
			}
		}
	}
</script>
<style lang="scss" scoped>
page{
	height: 100%;
	background-color: #f4f4f4;
	view{
		height: 100%;
		display: flex;
		flex-direction: column;
		align-items: center;
		//position: relative;
		.cover{
			width: 100%;
			height: 225rpx;
			border-radius: 0 0 40rpx 40rpx;
			overflow: hidden;
			//position: absolute;
			//top: 0;
			image{
				width: 100%;
				height: 100%;
			}
		}
		.topBar{
			display: flex;
			flex-direction: row;
			justify-content: space-evenly;
			height: 100rpx;
			width: 95%;
			background-color: white;
			border-radius: 10rpx;
			position: relative;
			top: -30rpx;
			z-index: 9;
			line-height: 90rpx;
			font-size: 28rpx;
			color: #333;
			text{
				margin: 0 20rpx;
			}
		}
		swiper{
			flex: 1;
			width: 100%;
			 swiper-item{
				 width: 100%;
				 height: 100%;
				 scroll-view{
				 	width: 100%;
					height: 100%;
					.content{
						height: fit-content;
						display: flex;
						flex-direction: column;
						.goodsList{
							width: 95%;
							display: flex;
							flex-direction: row;
							flex-wrap: wrap;
							justify-content: space-between;
							margin: 0 auto;
							height: fit-content;
								navigator{
								    width: 345rpx;
									padding: 20rpx;
									// padding: 20rpx 20rpx;
									display: flex;
									flex-direction: column;
									justify-content: space-around;
									align-items: flex-start;
									border-radius: 10rpx;
									background-color: #fff;
									box-sizing: border-box;
									margin-bottom: 20rpx;
									image{
										width: 305rpx;
										height: 305rpx;
									}
									.title{
										min-height: 88rpx;
										font-size: 26rpx;
									}
									.price{
										line-height: 1;
										color: #cf4444;
										font-size: 30rpx;
									}
								}
						}
						.load{
							flex: 1;
						}
					}
				 }
			 }
		}
	}
}	
</style>
